//
//  MusicViewController.m
//  LO_MUSIC
//
//  Created by lanou3g on 16/2/24.
//  Copyright © 2016年 杨少锋. All rights reserved.
//

#import "MusicViewController.h"
#import "RequestURL.h"
#import "DownloadImageOperation.h"
#import "MusicPlayViewController.h"
#import "AudioPlayer.h"


@interface MusicViewController ()<UITableViewDataSource,UITableViewDelegate,DownloadImageOperationDelegate>

@property (nonatomic,strong) UITableView *rootTableView;
//模型数组
@property (nonatomic,strong) NSArray *modelArray;

//队列
@property (nonatomic,strong) NSOperationQueue *queue;

//存放图片的url
@property (nonatomic,strong) NSMutableDictionary *ImageDic;

//存放正在下载的图片
@property (nonatomic,strong) NSMutableDictionary *operationDic;

////判断是否正在滑动
//@property (nonatomic,assign,getter=IsScrolling) BOOL Scrolling;

@end



@implementation MusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    
   
    
    self.rootTableView = [[UITableView alloc]initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    self.rootTableView.delegate = self;
    self.rootTableView.dataSource = self;
    
    [self.view addSubview:self.rootTableView];
    //请求数据
    __weak typeof (self)weakSelf = self;

    RequestURL *request = [RequestURL shareRequestUrl];
    
    [request requestMusiclistWithSuccess:^(id item) {
        
        weakSelf.modelArray = item;
        
        [weakSelf.rootTableView reloadData];
        
    } faile:^(NSError *err) {
        NSLog(@"%@",err);
    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.modelArray.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"uitableCell"];
    
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"uitableCell"];
    }
    
    MusicModel *model = self.modelArray[indexPath.row];
    cell.textLabel.text = model.name;
    cell.detailTextLabel.text = model.singer;
    
    //先去字典中查找
    if ([self.ImageDic.allKeys containsObject:model.picUrl]) {//字典中存在图片
        
        //从字典中取出图片给imageView赋值
        cell.imageView.image = self.ImageDic[model.picUrl];
        
    }else {
        
        cell.imageView.image = [UIImage imageNamed:@"1"];
        
        if (self.rootTableView.dragging || self.rootTableView.decelerating) {//如果正在滑动,不处理图片
            return cell;
            
        }
        //判断是不是正在下载图片
        if ([self.operationDic.allKeys containsObject:model.picUrl]) {//正在下载
            
            
        }else {//没有正在下载
        
            //初始化自定义线程块
            DownloadImageOperation *downImage = [[DownloadImageOperation alloc]init];
            
            downImage.url = model.picUrl;
            downImage.delegate = self;
            downImage.indexPath = indexPath;
            
            //添加到队列
            [self.queue addOperation:downImage];
            
            //添加到字典中
            [self.operationDic setValue:downImage forKey:downImage.url];
        }
        
        
    }
    
    return cell;
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 
    MusicPlayViewController *music = [[MusicPlayViewController alloc]init];
    [self.navigationController pushViewController:music animated:YES];
    
//    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    [music setValue:self.modelArray[indexPath.row] forKey:@"currentMusicModel"];
//    [music setValue:cell.imageView.image forKey:@"image"];
    [music setValue:indexPath forKey:@"indexPath"];
    
    [music setValue:self.modelArray forKey:@"modelArray"];
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

//滑动结束之后加载
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    if (!decelerate)//yes是减速过程中
    {
        
        [self loadImage];
    }
}

//结束减速
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView//减速结束
{
    
    [self loadImage];
    
}


-(void)loadImage{
    //取出当前可视的cell
    NSArray *visiblePaths = [_rootTableView indexPathsForVisibleRows];
    
    [self.rootTableView reloadRowsAtIndexPaths:visiblePaths withRowAnimation:(UITableViewRowAnimationNone)];
    
}


//DownloadImageDelegate

-(void)getImage:(UIImage *)image opreation:(DownloadImageOperation *)opreation{
    
    UITableViewCell *cell = [self.rootTableView cellForRowAtIndexPath:opreation.indexPath];
    cell.imageView.image = image;
    
    //从正在下载的字典中移除操作
    [self.operationDic removeObjectForKey:opreation.url];
    
    //图片下载下来之后放在字典中
//    self.ImageDic[opreation.url] = image;
    [self.ImageDic setValue:image forKey:opreation.url];
    
    [self.rootTableView reloadRowsAtIndexPaths:@[opreation.indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
}



#pragma mark 懒加载

-(NSOperationQueue *)queue {
    
    if (!_queue) {
        _queue = [[NSOperationQueue alloc]init];
        
        _queue.maxConcurrentOperationCount = 3;//最大并发数
    }
    return _queue;
}


-(NSMutableDictionary *)ImageDic{
    
    
    if (!_ImageDic) {
        _ImageDic = [NSMutableDictionary dictionary];
        
    }
    return _ImageDic;
    
}

-(NSMutableDictionary *)operationDic {
    
    
    if (!_operationDic) {
        _operationDic = [NSMutableDictionary dictionary];
    }
    
    return _operationDic;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
